#coding=utf-8
import requests
import os
import ConfigParser
from bs4 import BeautifulSoup
from cnkipagemongo import cnkipagemongo
class cnkiapplication:
    def __init__(self):
        self.headers={
            'Host':'shuyu.cnki.net',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0)Gecko/20100101 Firefox/55.0',
            'Accept':'*/*',
            'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding':'gzip, deflate',
            'Content-Type':'application/x-www-form-urlencoded; charset=utf-8',
            'Referer':'http://shuyu.cnki.net/NaviDetail.aspx?xk=AA&ids=A&zm=all',
            'Cookie':'ASP.NET_SessionId=iq3qq5zca2m4sfcst3fvactw; LID=;SID=201123',
            'Connection':'keep-alive'
        }
        self.data={
            '__EVENTTARGET':'',
            '__EVENTARGUMENT':'',
            '__VIEWSTATE':'',
            'ctl00$searchtext':'输入你想查询的术语（中英文均可）',
            'ctl00$ContentMiddleBrowser$hidBaseQID':30,
            'ctl00$ContentMiddleBrowser$hidXKId':'AA',
            'ctl00$ContentMiddleBrowser$hidZM':'all',
            'gridtable1_tbNum':1,
            '__CALLBACKID':'ctl00$ContentMiddleBrowser$gridtable1',
            '__CALLBACKPARAM':5,
            '__EVENTVALIDATION':''
        }
        self.url=None
        self.res=None
        self.soup=None
        self.cnkipagemongo=cnkipagemongo()
        config=ConfigParser.ConfigParser()
        path=os.path.split(os.path.realpath(__file__))[0] + '/cnkiconfig.conf'
        config.read(path)
        self.link=config.get("cnki","url")
        self.name=config.get('cnki','name')
        self.mold=config.get('cnki','type')
        self.count=config.get('cnki','count')

    def savepage(self):
        self.cnkipagemongo.savepage(self.res.text,self.url,self.mold,self.name)
       
    def changedata(self):
        __VIEWSTATE=self.soup.select("#__VIEWSTATE")[0].get('value')
        __EVENTVALIDATION=self.soup.select("#__EVENTVALIDATION")[0].get('value')
        QID=self.soup.select('#ctl00_ContentMiddleBrowser_hidBaseQID')[0].get('value')
        hidXKId=self.soup.select('#hidXKId')[0].get('value')
        hidZM=self.soup.select('#hidZM')[0].get('value')
        self.data['__VIEWSTATE']=__VIEWSTATE
        self.data['__EVENTVALIDATION']=__EVENTVALIDATION
        self.data['ctl00$ContentMiddleBrowser$hidBaseQID']=QID
        self.data['ctl00$ContentMiddleBrowser$hidXKId']=hidXKId
        self.data['ctl00$ContentMiddleBrowser$hidZM']=hidZM


    def start(self):
        self.res=requests.get(self.link,headers=self.headers)
        self.soup=BeautifulSoup(self.res.text,'lxml')
        self.changedata()
        self.url=self.link+'&page=1'
        print 'now url:'+self.url
        self.savepage()
        for i in range(2,int(self.count)):
            self.data['__CALLBACKPARAM']=i
            self.res=requests.post(self.link,headers=self.headers,data=self.data)
            self.url=self.link+'&page='+str(i)
            print 'now url:'+self.url
            self.savepage()



if __name__ =="__main__":
    cnkiapplication=cnkiapplication()
    cnkiapplication.start()
